/**
 * 自定义函数
 * */


//列表
function getList(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var pageStart = getParamByUrl('start');
    pageStart = pageStart ? pageStart : pages.start;
    var formData = {
        uid: uinfo.uid,
        //role_id: uinfo.role_id,
        depart_id: uinfo.depart_id,
        start: pageStart, limit: pages.limit,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'tongji/all',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.start = pageStart;
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i, ii, whList;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].item_name + '</td>';
        h += '<td>';
        whList = d[i].whList;
        if (whList.length > 0) {
            for (ii = 0; ii < whList.length; ii ++) {
                h += '<div>';
                h += whList[ii].uname;
                h += '，' + whList[ii].wh_name;
                h += '，' + whList[ii].delay_str;
                h += '<div>';
            }
        } else {
            h += '-';
        }
        h += '</td>';
        h += '<td>' + d[i].delay_str + '</td>';
        h += '</tr>';
    }
    return h;
}


// 跳转
function toJump(){
    var depart_id = $('#depart_id').val();
    var staff_id = $('#staff_id').val();
    var month_id = $('#month_id').val();
    var p = '?depart_id=' + depart_id;
    p += '&staff_id=' + staff_id;
    p += '&month_id=' + month_id;
    window.location.href = 'all.html' + p;
}


//选项
function getOption(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var formData = {
        uid: uinfo.uid,
        role_id: uinfo.role_id,
        depart_id: uinfo.depart_id,
    };
    if (depart_id > 0) {
        formData.depart_id = depart_id;
    }
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'tongji/option',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                return
            }
            var staff = res.data.staff;
            var depart = res.data.depart;
            var h = '', i;
            //部门
            if (depart_id == 0) {
                h = '<option value="0" selected>-选择部门-</option>';
            } else {
                h = '<option value="0">-选择部门-</option>';
            }
            for (i = 0; i < depart.length; i ++) {
                if (depart_id == depart[i].id) {
                    h += '<option value="' + depart[i].id + '" selected>';
                } else {
                    h += '<option value="' + depart[i].id + '">';
                }
                h += depart[i].title;
                h += '</option>';
            }
            $('#depart_id').html(h);
            //员工
            if (staff_id == 0) {
                h = '<option value="0" selected>-选择员工-</option>';
            } else {
                h = '<option value="0">-选择员工-</option>';
            }
            for (i = 0; i < staff.length; i ++) {
                if (staff_id == staff[i].id) {
                    h += '<option value="' + staff[i].id + '" selected>';
                } else {
                    h += '<option value="' + staff[i].id + '">';
                }
                h += staff[i].title;
                h += '</option>';
            }
            $('#staff_id').html(h);
            //月份
            if (month_id == 0) {
                // 
            } else {
                // 
            }
            h = '<option value="0">选择</option>';
            for (i = 1; i <= 12; i ++) {
                if (month_id == i) {
                    h += '<option value="' + i + '" selected>';
                } else {
                    h += '<option value="' + i + '">';
                }
                h += i;
                h += '</option>';
            }
            $('#month_id').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}